﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FtpWebRole.Entities;
using Microsoft.WindowsAzure;
using System.Drawing;

namespace FtpWebRole
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            LoadUsers();
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            // validation
            if (string.IsNullOrWhiteSpace(TextBox1.Text))
            {
                Label1.Text = "Please input the name of the user.";
                Label1.ForeColor = Color.Red;
                return;
            }
            else if (string.IsNullOrWhiteSpace(TbxPswd1.Text))
            {
                Label1.Text = "The password can't be empty.";
                Label1.ForeColor = Color.Red;
                return;
            }
            else if (TbxPswd1.Text != TbxPswd2.Text)
            {
                Label1.Text = "The password and confirmation password must match.";
                Label1.ForeColor = Color.Red;
                return;
            }
            var ctx = new UserContext(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"));
            IQueryable<UserEntity> temp = ctx.UserEntities;
            if (temp.FirstOrDefault() != null)
            {
                if (temp.Where(al => al.Name == TextBox1.Text).FirstOrDefault() != null)
                {
                    Label1.Text = "Name is existed.";
                    Label1.ForeColor = Color.Red;
                    return;
                }
            }

            // create new user
            UserEntity u = new UserEntity(TextBox1.Text);
            u.Name = TextBox1.Text;
            u.Password = TbxPswd1.Text;
            u.IsShared = false;
            ctx.AddUser(u);
            Label1.Text = "Add user successfully.";
            Label1.ForeColor = Color.Green;

            // reload
            LoadUsers();
        }

        private void LoadUsers()
        {
            var ctx = new UserContext(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"));
            GridView1.DataSource = ctx.UserEntities;
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // determine the user name to be deleted
            string name = GridView1.Rows[e.RowIndex].Cells[0].Text;

            // find the corresponding one in the context and delete
            var ctx = new UserContext(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"));
            var real = ctx.UserEntities
                .Where(u => u.Name == name)
                .FirstOrDefault();
            ctx.DeleteUser(real);

            // refresh the page
            LoadUsers();
        }

        protected void Rbtn_CheckedChanged(object sender, EventArgs e)
        {
            // determine which row
            int index = -1;
            bool flag = false;
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.FindControl("RbtnShared") == sender)
                {
                    index = row.RowIndex;
                    flag = true;
                    break;
                }
                else if (row.FindControl("RbtnPrivate") == sender)
                {
                    index = row.RowIndex;
                    flag = false;
                    break;
                }
            }
            if (index == -1) return;

            // determine the user name to be updated
            string name = GridView1.Rows[index].Cells[0].Text;
            
            // find the corresponding one in the context and update
            var ctx = new UserContext(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"));
            var real = ctx.UserEntities
                 .Where(u => u.Name == name)
                 .FirstOrDefault();
            real.IsShared = flag;
            ctx.UpdateUser(real);
 
            // don't refresh the page
            // LoadUsers();
        }
    }
}